package com.spzx.user.controller;

import java.util.List;
import java.util.Arrays;

import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.domain.UserBrowseHistory;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.domain.UserInfoVo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.spzx.common.core.web.page.TableDataInfo;

/**
 * 会员信息Controller
 *
 * @author atguigu
 * @date 2024-08-03
 */
@Tag(name = "会员信息接口管理")
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {
    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 查询会员信息列表
     */
    @Operation(summary = "查询会员信息列表")
    @RequiresPermissions("user:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(UserInfo userInfo) {
        startPage();
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        return getDataTable(list);
    }

    /**
     * 获取用户地址信息
     *
     * @param userId 用户ID
     * @return 用户地址列表
     */
    @Operation(summary = "获取用户地址")
    @RequiresPermissions("user:userInfo:query")
    @GetMapping(value = "getUserAddress/{userId}")
    public AjaxResult getUserAddress(@PathVariable("userId") Long userId) {
        return success(userInfoService.selectUserAddressList(userId));
    }

    /**
     * 导出会员信息列表
     */
    @Operation(summary = "导出会员信息列表")
    @RequiresPermissions("user:userInfo:export")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserInfo userInfo) {
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        ExcelUtil<UserInfo> util = new ExcelUtil<UserInfo>(UserInfo.class);
        util.exportExcel(response, list, "会员信息数据");
    }

//    /**
//     * 获取会员信息详细信息
//     */
//    @Operation(summary = "获取会员信息详细信息")
//    @RequiresPermissions("user:userInfo:query")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(userInfoService.getById(id));
//    }

    /**
     * 新增会员信息
     */
    @Operation(summary = "新增会员信息")
    @RequiresPermissions("user:userInfo:add")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.save(userInfo));
    }

    /**
     * 修改会员信息
     */
    @Operation(summary = "修改会员信息")
    @RequiresPermissions("user:userInfo:edit")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.updateById(userInfo));
    }

    /**
     * 删除会员信息
     */
    @Operation(summary = "删除会员信息")
    @RequiresPermissions("user:userInfo:remove")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userInfoService.removeBatchByIds(Arrays.asList(ids)));
    }

    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody UserInfo userInfo) {
        userInfoService.register(userInfo);
        return R.ok();
    }

    @Operation(summary = "更新用户登录信息")
    @InnerAuth
    @PutMapping("/updateUserLogin")
    public R<Boolean> updateUserLogin(@RequestBody UpdateUserLogin updateUserLogin) {
        return R.ok(userInfoService.updateUserLogin(updateUserLogin));
    }

    @Operation(summary = "根据用户名获取用户信息")
    @InnerAuth
    @GetMapping("/info/{username}")
    public R<UserInfo> getUserInfo(@PathVariable("username") String username) {
        UserInfo userInfo = userInfoService.selectUserByUserName(username);
        return R.ok(userInfo);
    }

    @Operation(summary = "获取当前登录用户信息")
    @RequiresLogin
    @GetMapping("/getLoginUserInfo")
    public AjaxResult getLoginUserInfo(HttpServletRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return success(userInfoVo);
    }

    @Operation(summary = "判断商品是否收藏")
    @RequiresLogin
    @GetMapping("/isCollect/{skuId}")
    public AjaxResult isCollect(@PathVariable("skuId") Long skuId) {
        return success(userInfoService.isCollect(skuId));
    }

    @Operation(summary = "收藏商品")
    @RequiresLogin
    @GetMapping("/collect/{skuId}")
    public AjaxResult collect(@PathVariable("skuId") Long skuId) {
        return success(userInfoService.collect(skuId));
    }

    @Operation(summary = "取消收藏")
    @RequiresLogin
    @GetMapping("/auth/cancelCollect/{skuId}")
    public AjaxResult cancelCollect(@PathVariable("skuId") Long skuId) {
        return success(userInfoService.cancelCollect(skuId));
    }

    @Operation(summary = "用户收藏分页列表查询")
    @RequiresLogin
    @GetMapping("/userCollectList/{pageNum}/{pageSize}")
    public TableDataInfo userCollectList(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        List<UserCollect> userCollectList = userInfoService.userCollectList(pageNum, pageSize);
        return getDataTable(userCollectList);
    }

    @Operation(summary = "商品浏览历史")
    @RequiresLogin
    @GetMapping("/userBrowseHistoryList/{pageNum}/{pageSize}")
    public TableDataInfo userBrowseHistoryList(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        List<UserBrowseHistory> userBrowseHistoryList = userInfoService.userBrowseHistoryList(pageNum, pageSize);
        return getDataTable(userBrowseHistoryList);
    }
}
